<?php

session_start();

header('Access-Control-Allow-Origin:*');

date_default_timezone_set("PRC");

header("Content-Type: text/html;charset=utf8");

require_once '../common/in_mysql.php';
require_once '../db/code.db.php';

$action = filter_input(INPUT_POST, "action") ? filter_input(INPUT_POST, "action") : filter_input(INPUT_GET, "action");

$res = array();
switch ($action) {
    case 'listCode':
        $res = listCode();
        break;
    case 'CreateCode':
        $res = createCode();
        break;
    case 'updateDela':
        $res = updateDelay();
        break;
    case 'createGroupKey':
        $res = createGroupKey();
        break;
    case 'createGroupKeyActive':
        $res = createGroupKeyActive();
        break;
    case 'getStudentCode':
        $res = getStudentCode();
        break;
    case 'createStudentCode':
        $res = createStudentCode();
        break;

    default :
}
echo json_encode($res);

function listCode()
{
    $cs = filter_input(INPUT_POST, "Cs");
    $isActive = filter_input(INPUT_POST, "Active");
    $page = filter_input(INPUT_POST, "page");
    $pageSize = filter_input(INPUT_POST, "limit");
    $type = filter_input(INPUT_POST, "type");
    $keyword = filter_input(INPUT_POST, "word");

    if ($cs == "" || (!is_numeric($cs))) {
        $cs = -1;
    }
    if ($isActive == "" || (!is_numeric($isActive))) {
        $isActive = -1;
    }

    if ($page == "" || (!is_numeric($page))) {
        $page = 0;
    } else if ($page > 0) {
        $page--;
    }
    if ($pageSize == "" || (!is_numeric($pageSize))) {
        $pageSize = 100;
    }

    $codeDb = new CodeDb();
    $data = $codeDb->listCode($cs, $isActive, $page, $pageSize, $type, $keyword);

    $array = $data['data'];
    for ($i = 0; $i < count($array); $i++) {
        $array[$i]['vol'] = $array[$i]['cs'];
        $array[$i]['Mac'] = $array[$i]['mac'];
        unset($array[$i]['cs']);
        unset($array[$i]['mac']);
    }

    $res['code'] = 0;
    $res['msg'] = "";
    $res['data'] = $array;
    $res['count'] = $data['count'];

    return $res;
}

function getStudentCode()
{
    $page = filter_input(INPUT_POST, "page");
    $pageSize = filter_input(INPUT_POST, "limit");
    $codeDb = new CodeDb();
    $page--;
    $data = $codeDb->getStudentCode($page, $pageSize);

    $array = $data['data'];
    for ($i = 0; $i < count($array); $i++) {
        $array[$i]['Exptime'] = date("Y-m-d H:i",$array[$i]['exptime']);
        unset($array[$i]['exptime']);
    }

    $res['code'] = 0;
    $res['msg'] = "";
    $res['data'] = $array;
    $res['count'] = $data['count'];

    return $res;
}

function createCode()
{
    $cs = filter_input(INPUT_POST, "vol");
    $day = filter_input(INPUT_POST, "day");
    $count = filter_input(INPUT_POST, "num");

    if ($cs == "" || (!is_numeric($cs)) || $cs < 0 || $cs > 5) {
        $res['code'] = 111;
        $res['msg'] = "参数cs只能为0到5的正整数";

        return $res;
    }

    if ($day == "" || (!is_numeric($day)) || $day < 1) {
        $res['code'] = 112;
        $res['msg'] = "参数day只能为正整数";
        return $res;
    }

    if ($count == "" || (!is_numeric($count)) || $count < 1) {
        $res['code'] = 113;
        $res['msg'] = "参数count只能为正整数";
        $res['count'] = $_POST;
        return $res;
    }

    $exp = $day * 86400; //86400 一天对应的秒

    $codeDb = new CodeDb();
    $dat = $codeDb->createCode($cs, $exp, $count);
    $countData = count($dat);
    if ($dat) {
        $res['code'] = 0;
        $res['msg'] = "新增成功";
        $res['data'] = $dat;
        $res['count'] = $countData;
    } else {
        $res['code'] = 205;
        $res['msg'] = "创建激活码失败";
    }
    return $res;
}

function updateDelay()
{
    $id = filter_input(INPUT_POST, "cId");
    $endTime = filter_input(INPUT_POST, "eTime");
    $codeDb = new CodeDb();
    if ($codeDb->updateDelay($id, $endTime)) {
        $res['code'] = 0;
        $res['msg'] = "修改成功";
    } else {
        $res['code'] = 205;
        $res['msg'] = "修改失败";
    }
    return $res;
}

/**
 * 创建一组激活码，从第一册到第六册
 */
function createGroupKey()
{
    $cs = $_POST['Cs'];
    $info_comments = $_POST['info_comments'];
    $range_num = $_POST['Much'];

    $range_time = $_POST['shiJian'];
    $num = $_POST['shuLiang'];

    if (!is_numeric($range_num)) {
        $res['code'] = 302;
        $res['msg'] = "输入的时间不是数字";
        return $res;
    }

    $info_comments = trim($info_comments);
    $exp = 0; //过期时间 单位：秒
    if ($range_time == "year") {
        $exp = $range_num * 31536000; //31536000 一年对应的秒
    } else if ($range_time == "month") {
        $exp = $range_num * 2592000; //2592000 一月对应的秒
    } else if ($range_time == "day") {
        $exp = $range_num * 86400; //86400 一天对应的秒
    }
    $codeDb = new CodeDb();
    $data = $codeDb->createGroupKeys($cs, $exp, $info_comments);
//    if ($data['code']==0) {
//
//        $res['code'] = 0;
//        $res['data']=$data['data'];
//        $res['msg'] = "新增成功";
//    } else {
//        $res['code'] = 205;
//        $res['msg'] = "新增失败";
//    }
    return $data;
}


/**
 * 创建一组激活码，从第一册到第六册 并激活
 */
function createGroupKeyActive()
{
    $cs = $_POST['Cs'];
    $info_comments = $_POST['info_comments'];
    $range_num = $_POST['Much'];
    $range_time = $_POST['shiJian'];
    $num = $_POST['shuLiang'];

    $mac = $_POST['mac'];
    $codeDb = new CodeDb();
    if (!is_numeric($range_num)) {
        $res['code'] = 302;
        $res['msg'] = "输入的时间不是数字";
        return $res;
    }

    $info_comments = trim($info_comments);
    $exp = 0; //过期时间 单位：秒
    if ($range_time == "year") {
        $exp = $range_num * 31536000; //31536000 一年对应的秒
    } else if ($range_time == "month") {
        $exp = $range_num * 2592000; //2592000 一月对应的秒
    } else if ($range_time == "day") {
        $exp = $range_num * 86400; //86400 一天对应的秒
    }
    $acttime = time();
    $endtime = $acttime + $exp;

    $data = $codeDb->createGroupKeysActive($cs, $exp, $info_comments, $mac, $acttime, $endtime);
    return $data;
}

function createStudentCode()
{
    $num = filter_input(INPUT_POST, "UserNumber");//次数
    $codeNumber = filter_input(INPUT_POST, "CodeNumber");//个数
    $desc = filter_input(INPUT_POST, "info_comments");//说明
    $range_num = filter_input(INPUT_POST, "Much");//有效期
    $range_time = filter_input(INPUT_POST, "shiJian");//日期
    if ($range_time == "year") {
        $exp = $range_num * 31536000; //31536000 一年对应的秒
    } else if ($range_time == "month") {
        $exp = $range_num * 2592000; //2592000 一月对应的秒
    } else if ($range_time == "day") {
        $exp = $range_num * 86400; //86400 一天对应的秒
    }

    $codeDb = new CodeDb();
    $exp=$exp+time();
    $acttime = date("Y-m-d H:i", time());
    $dat = $codeDb->createCodeS($codeNumber, $num, $exp, $desc, $acttime);
    $countData = count($dat);
    if ($dat) {
        $res['code'] = 0;
        $res['msg'] = "新增成功";
        $res['data'] = $dat;
        $res['count'] = $countData;
    } else {
        $res['code'] = 205;
        $res['msg'] = "创建激活码失败";
    }
    return $res;
}